package com.dragon.client.controller.supplies;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.supplies.IProductUnitService;
import com.dragon.repository.domain.dto.ProductUnitDTO;
import com.dragon.repository.domain.vo.ProductUnitVO;
import com.dragon.repository.entity.ProductUnit;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品单位 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-09-18
 */
@RestController
@RequestMapping("productUnit")
@Api(tags = "产品单位")
public class ProductUnitController {

    @Autowired
    IProductUnitService service;

    @GetMapping("list")
    @ApiOperation(value = "获取所有的产品单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "state", value = "状态", required = false, type = "int")
    })
    public ResponseData<List<ProductUnitDTO>> getList(@RequestParam Integer page,
                                                      @RequestParam Integer limit,
                                                      @RequestParam(required = false) Integer state){
        Page<ProductUnit> unitPage = service.getList(page, limit, state);
        return ResponseData.success(ProductUnitDTO.convertList(unitPage.getRecords()),unitPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public ResponseData addUnit(@RequestBody ProductUnitVO vo){
        service.addUnit(ProductUnitVO.convert(vo));
        return ResponseData.success();
    }
    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updateUnit(@RequestBody ProductUnitVO vo){
        service.updateUnit(ProductUnitVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData deleteUnit(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                   @RequestParam(required = false) Integer id){
        service.deleteUnit(id);
        return ResponseData.success();
    }

}
